
package com.lee.utils; 
 
import com.alibaba.fastjson.JSON; 
import com.alibaba.fastjson.JSONArray; 
import com.lee.source.HbaseSource; 
 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.stream.Collectors; 
 
public class FrontKMapUtils { 
    private static String tableName = "marketing:frontkmap";
    private static String family = "info";
    private static String qualifier = "info";
    public static void initMap() throws IOException {
        HbaseSource.truncate(tableName);
    }
    public static boolean judgeKeyInMap(List<String> key) throws IOException {
        key.sort(String::compareTo);
        String result = HbaseSource.get(key.toString(),tableName,family,qualifier);
        if(! "".equals(result)){
            return true;
        }else {
            return false;
        }
 
    }
    public static void addItemToMap(List<String> key,List<String> item) throws IOException {
        key.sort(String::compareTo);
        if(judgeKeyInMap(key)){
            List<List<String>> data = getValueByKey(key);
            if(!ArrayUtils.isListItemInArray(item,data)) {
                data.add(item);
 
                String dataStr = JSON.toJSONString(data);
                HbaseSource.put(key.toString(), tableName, family, qualifier, dataStr);
            }
        }else {
            List<List<String>> data = new ArrayList<>();
            data.add(item);
            String dataStr = JSON.toJSONString(data);
            HbaseSource.put(key.toString(),tableName,family,qualifier,dataStr);
        }
    }
    public static List getValueByKey(List<String> key) throws IOException {
        key.sort(String::compareTo);
        String result = HbaseSource.get(key.toString(),tableName,family,qualifier);
        List<String> dataRaw = JSONArray.parseArray(result).toJavaList(String.class);
        List<List<String>> data = dataRaw.stream().map(ritem->{
            List<String> dataItem = JSONArray.parseArray(ritem).toJavaList(String.class);
            return dataItem;
        }).collect(Collectors.toList());
        return data;
    }
 
 
} 
